
const successFunction = function (result, callback) {
    if (callback) {
        callback(result);
    } else {
        if (result.code === 401) {
            window.vue.$message({
                message: result.msg,
                type: 'warn'
            });
        } else if (result.code === 500) {
            window.vue.$confirm(result.msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '',
                showCancelButton: false,
                closeOnClickModal: false,
                closeOnPressEscape: false,
                closeOnHashChange: false,
                lockScroll: true,
                type: 'warning',
                showClose: false,
            }).then(() => {
                window.location.href = "/login";
            });
        }
    }
}

const errorFunction = function (error) {
    window.vue.$message({
        message: "请检查网络~",
        type: 'warn'
    });
}

// 重复代码抽取
const postRequest = function (url, data, callback) {
    $.ajax({
        url: url, //要请求的后端地址
        type: "POST", //数据发送的方式(POST或者GET)
        contentType: "application/json",
        dataType: "json", //后端返回的数据格式
        data: JSON.stringify(data), //需要传递的参数
        success: function (result) {
            successFunction(result, callback);
        },
        error: errorFunction
    });
}

const getRequest = function (url, callback) {
    $.ajax({
        url: url, //要请求的后端地址
        type: "GET", //数据发送的方式(POST或者GET)
        success: function (result) {
            successFunction(result, callback);
        },
        error: errorFunction
    });
}